<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button style="float: left;" type="primary" @click="addbtn">添加房产</el-button>

        <el-button style="float:right;marginLeft:0">筛选</el-button>
        <el-input style="width:400px; float:right" />
        <el-select v-model="agentValue" placeholder="全部经纪" style="width:160px;float:right">
          <el-option v-for="item in agentOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select v-model="categoryValue" placeholder="全部分类" style="width:160px;float:right">
          <el-option v-for="item in categoryOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column

            label="封面"
            width="134.7px"
          >
            <template v-slot="obj">
              <img class="cover" :src="obj.row.image" alt="">
            </template>

          </el-table-column>
          <el-table-column
            prop="title"
            label="标题"
            width="134.7px"
          />
          <el-table-column

            label="标签"
            width="134.7px"
          >
            <template v-slot="obj">
              <el-tag
                v-for="tag in obj.row.tags"
                :key="tag"
                type="success"
              >{{ tag }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            label="地址"
            width="134.7px"
          />

          <el-table-column
            prop="category.title"
            label="分类"
            width="134.7px"
          />
          <el-table-column
            prop="agent.name"
            label="经纪"
            width="134.7px"
          />
          <el-table-column
            prop="price"
            label="价格"
            width="134.7px"
          />
          <el-table-column
            prop="agent.desc"
            label="介绍"
            width="134.7px"
          />
          <el-table-column

            label="图片"
            width="134.7px"
          >
            <template v-slot="obj">
              <img v-for="item in obj.row.photos" :key="item" class="cover" :src="item" alt="">
            <!-- {{ obj.row }} -->
            </template>

          </el-table-column>
          <el-table-column

            label="操作"
            width="134.7px"
          >
            <template v-slot="obj">
              <el-button type="primary" size="small" style="width:50px" @click="btnEdit(obj.row.id)">编辑</el-button>
              <el-button type="danger" size="small" style="width:50px" @click="btnDel(obj.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination :total="total" :page-size="pageConfig._limit" layout="prev, pager, next" @current-change="currentChange" />
      </el-row>
    </el-card>
    <!-- 弹窗 -->
    <elDialogAdd ref="elDialogAdd" :show-dialog="isShowDialog" @closeDialog="isShowDialog=false" />
  </div>
</template>

<script>
import { getProperties, delProperty, getPropertyCount, getPropertyById } from '@/api/properties'
import elDialogAdd from './components/el_dialog.vue'
import { getCategories } from '@/api/categories'
import { jingji } from '@/api/jingji'
export default {
  components: {
    elDialogAdd
  },
  data() {
    return {
      // 经纪数据
      agentOption: [],
      agentValue: '',
      // 分类数据
      categoryOption: [],
      categoryValue: '',
      // 分页
      total: 0,
      pageConfig: {
        _limit: 2,
        _start: 0
      },
      isShowDialog: false,
      tableData: []

    }
  },
  created() {
    this.loadPage()
    this.getCategories()
    this.getAgents()
  },
  methods: {
    // 获取分类
    async getCategories() {
      const { data } = await getCategories()
      // console.log(145, res)
      data.forEach(item => {
        const cateObj = { value: item.id, label: item.title }
        this.categoryOption.push(cateObj)
      })
    },
    // 获取经纪
    async getAgents() {
      const { data } = await jingji()
      // console.log(154, data)
      data.forEach(item => {
        const agentObj = { value: item.id, label: item.name }
        this.agentOption.push(agentObj)
      })
      // console.log(160, this.agentOption)
    },
    // 获取房产数量
    async  getTotal() {
      const res = await getPropertyCount()
      // console.log(136, res)
      this.total = res.data
    },
    // 分页
    currentChange(newPage) {
      this.pageConfig._start = (newPage - 1) * this.pageConfig._limit
      this.loadPage()
    },
    // 编辑
    async btnEdit(id) {
      // 发请求
      const { data } = await getPropertyById(id)
      // console.log(data)
      this.isShowDialog = true
      this.$nextTick(() => {
        this.$refs.elDialogAdd.formData = data
      })
    },
    // 页面渲染
    async loadPage() {
      const res = await getProperties(this.pageConfig)
      console.log(157, res)
      this.tableData = res.data
      this.getTotal()
    },
    // 删除
    async  btnDel(id) {
      // console.log(id)
      try {
        // 二次咨询
        await this.$confirm('是否删除?')
        // 发请求
        await delProperty(id)
        // 提示
        this.$message.success('操作成功')
        // 重新加载页面
        this.loadPage()
      } catch (error) {
        console.log(error)
      }
    },
    // 新增按钮
    async addbtn() {
      this.isShowDialog = true
    }
  }
}
</script>

<style>
.cover{
  width: 112px;
height: 88px;
 /* object-fit: cover; */
object-fit: cover;
}
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {

    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

</style>
